package org.datavis.renderer;

import org.datavis.chart.Chart;
import org.datavis.data.Data;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.DrawFilter;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.view.View;

public abstract class ChartRenderer extends View  {
	
	protected Canvas canvas = null;
	protected Bitmap bitmap = null;
	protected int origin = 0;
	protected Data data = null;
	protected Data subData = null;
	protected Data origData = null;
	protected Chart chart = null;
	protected int selectionX = 0;
	protected int selectionDX = 0;
	protected Paint mPaint;
	protected int intervalSize = 0;
		
	protected int[] colors = {0xFF005aa6, 0xFFf39a00, 0xFF4e940a, 0xFF482b42};
		 
	public ChartRenderer(Context context) {
		super(context);
		
    }
	
	public ChartRenderer(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public ChartRenderer(Context context, Chart chart) {
		super(context);
		this.chart = chart;
		
	}
	
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (bitmap != null) {
            bitmap .recycle();
        }
        if(w > 0 && h > 0)
        {
	        canvas = new Canvas();
	        DrawFilter drawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG);
	        canvas.setDrawFilter(drawFilter);
	        bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
	        canvas.setBitmap(bitmap);
	        //this.setOnTouchListener(new ChartTouchListener(this, chart));
        }
    }
    
    public void destroy() {
        if (bitmap != null) {
            bitmap.recycle();
        }
    }
    
    @Override
    public void onDraw(Canvas c) {    	
    	data = chart.getData();
    	//calculate origin
    	if(data != null)
    	{			
			if(data.getMaxValue()> 0. && (data.getMinValue() < 0.))
			{
				origin = (int)(this.getHeight() * (data.getMaxValue()/data.getValueRange()));
			}
			else if (data.getMaxValue() > 0.)
			{
				origin = this.getHeight();
			}
			else if (data.getMaxValue() < 0)
			{
				origin = 0;
			}
    	}
    }
    
    protected int calculateHeight(double maxValue, double value){
    	int height = 0;    	    	    	
    	
    	if(value < 0.)
    		value = -value;
    	 
    	height = (int)(this.getHeight() * (value/maxValue));
    	
    	return height;
    }

	public Canvas getCanvas() {
		return canvas;
	}

	public void setSelection(int x, int dx) {
		selectionX = x;
		selectionDX = dx;
	}

	public void evaluateTouchEvent()
	{
		if(selectionDX < selectionX && origData != null)
			resetOriginalData();
		if(selectionDX > selectionX && origData == null)
			setSubData();
	}
	
	//reset data after "back" touch event
	protected void resetOriginalData()
	{
		chart.setData(origData.clone());
		data = chart.getData();
		origData = null;
		subData = null;
		selectionX = 0;
		selectionDX = 0;
	}
	
	//set new data after "selcetion" touch event
	protected void setSubData()
	{
		if((selectionDX - selectionX) > intervalSize)
		{
			origData = data.clone();
			subData = data.clone();
			int x = 0;
			int length = subData.getValuesPerEntry();
			for(int i = length -1; i >= 0 ; i--)
			{
				if(((intervalSize/2) + (i*intervalSize)) < selectionX)
				{
					subData.removeData(0);
					x++;
				}
				if (((intervalSize/2) + (i*intervalSize)) > selectionDX)
					subData.removeData(i);			
			}
			
			selectionX = 0;
			selectionDX = 0;
			chart.setData(subData.clone());
		}
		else
		{
			selectionX = 0;
			selectionDX = 0;
			this.invalidate();
		}
	}
	
	// draw rectangle while motion touch event
	protected void setDrawSelectionRect()
	{
		if(selectionDX > selectionX && origData == null)
		{
			mPaint.setColor(0x44000000);  
			mPaint.setStyle(Paint.Style.FILL);
			canvas.drawRect(selectionX, 0, selectionDX, this.getHeight(), mPaint);
		}
	}
	
    
}
